<?php

/**
 * detalle actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage detalle
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class detalleActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->proyectos = Doctrine_Core::getTable('proyecto')
                ->createQuery('a')
                ->execute();
        $this->fondos = Doctrine_Core::getTable('fondo')
                ->createQuery('a')
                ->execute();
    }

    public function executeCronogramaFondo(sfWebRequest $request) {
        
        $idfo = $request->getParameter('id_fo');
        $this->getUser()->setAttribute('id_fo', $idfo);
        

        $f = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idfo);
        $this->fondo = $f->fetchOne();
        $this->getUser()->setAttribute('id_fo', $idfo);

        $p = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->fondo->getIdPro());
        $this->proyecto = $p->fetchOne();
        $this->getUser()->setAttribute('id_prore', $this->proyecto->getIdPro());


        $this->objetivos = Doctrine_Core::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
        
         $this->techos = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
         
         $this->descripciones = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                ->where('d.id_pro = ?', $this->fondo->getIdPro())
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->proyecto = Doctrine_Core::getTable('proyecto')->find(array($request->getParameter('id_pro')));
        $this->forward404Unless($this->proyecto);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new proyectoForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new proyectoForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($proyecto = Doctrine_Core::getTable('proyecto')->find(array($request->getParameter('id_pro'))), sprintf('Object proyecto does not exist (%s).', $request->getParameter('id_pro')));
        $this->form = new proyectoForm($proyecto);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($proyecto = Doctrine_Core::getTable('proyecto')->find(array($request->getParameter('id_pro'))), sprintf('Object proyecto does not exist (%s).', $request->getParameter('id_pro')));
        $this->form = new proyectoForm($proyecto);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($proyecto = Doctrine_Core::getTable('proyecto')->find(array($request->getParameter('id_pro'))), sprintf('Object proyecto does not exist (%s).', $request->getParameter('id_pro')));
        $proyecto->delete();

        $this->redirect('detalle/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $proyecto = $form->save();

            $this->redirect('detalle/edit?id_pro=' . $proyecto->getIdPro());
        }
    }

}
